function after_episode(episode_num,time_elapsed,winner)

% structures for player memory
global actions;
global running;
global timers;

global actions_op;
global running_op;
global timers_op;

% learning related
global reward;      % immediate reward from state
global alpha;       % step-size constant
global delta;       % change
global epsilon;     % constant for epsilon-greedy selection
global gamma;       % discount rate
global lambda;      % eligibility trace value
global theta;       % parameters vector
global e;           % eligibility trace
global Fa;          % feature vector
global Qa;          % Q-value of current action

global system_mode;
global red;
global blue;
global time;
global total_reward;

global num_players_r;
global num_players_b;

if (winner == 'r')
    reward = 100;
    red = red + 1;
else
    reward = -100;
    blue = blue + 1;
end

total_reward = total_reward + num_players_r*reward;

if strcmp(system_mode, 'learn') || strcmp(system_mode, 'load')
    
    for P_num = 1:num_players_r,
        % get the last feature vector
        F = Fa{P_num};
        Q = Qvalue(F, theta(P_num,:));
        % get the reward and compute the delta
        delta = reward - Q;
        
        % perform last update on parameter vector
        theta(P_num,:) = theta(P_num,:) + alpha*delta*e(P_num,:);
    end
    
    % save intermediate policies
	if(ismember(episode_num, [10,30,50,100,150,200,250,300,400,500,600,800,1000,1200,1400,1600,1800, 2000, 2250, 2500, 2750, 3000, 3250, 3500, 3750, 4000, 4250, 4500, 4750, 5000, 5500, 6000, 6500, 7000, 7500, 8000, 8500, 9000, 9500, 10000]) || rem(episode_num,500)==0)
        SavePolicy();
	end
end

if(rem(episode_num, 10) == 0)
    episode_num % display episode number in the matlab terminal

    if(rem(episode_num, 100) == 0)
        epsilon = 0.99*epsilon; % reduce the epsilon parameter
        disp(sprintf('epsilon: %s gamma: %s lambda: %s alpha: %s', num2str(epsilon), num2str(gamma), num2str(lambda), num2str(alpha)));
    end

end

% reset the variables related to remembering the selected macro-action
running = zeros(1, num_players_r);
actions = zeros(1, num_players_r);
timers = zeros(1, num_players_r);

running_op = zeros(1, num_players_b);
actions_op = zeros(1, num_players_b);
timers_op = zeros(1, num_players_b);

end
